﻿@model IEnumerable<Models.ItemList>

@{
    Layout = null;
    var cashs = ViewBag.Cash as List<Models.ItemList>;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <environment include="Development">
        <link rel="stylesheet" href="~/layer/css/layui.css" media="all" />
        <link rel="stylesheet" href="~/layer/css/admin.css" media="all" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="~/layer/css/layui.css" asp-append-version="true" media="all" />
        <link rel="stylesheet" href="~/layer/css/admin.css" asp-append-version="true" media="all" />
    </environment>
</head>
<body id="iosiframe">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">订单列表</div>
                    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">订单状态</label>
                                <div class="layui-input-block">
                                    <select name="status">
                                        @foreach (var m in Model) {
                                            <option value="@m.value">@m.text</option>
                                        }
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="layui-card-body">
                        <table class="layui-hide" id="table-page" lay-filter="table" lay-size="lg"></table>

                        @if ((int)ViewData["userType"] == (int)Code.UserTypes.普通用户) {
                            <script type="text/html" id="buttonTpl">
                                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="look"><i class="layui-icon layui-icon-search"></i>查看</a>
                                {{# if(d.Status  >= 3){}}
                                <a class="layui-btn layui-btn-disabled layui-btn-xs" lay-event="re">汇款</a>
                                <a class="layui-btn layui-btn-disabled layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>取消</a>
                                {{# } else { }}
                                <a class="layui-btn layui-btn-xs" lay-event="re">汇款</a>
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>取消</a>
                                {{# } }}
                            </script>
                        } else {
                            <script type="text/html" id="buttonTpl">
                                <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="look"><i class="layui-icon layui-icon-search"></i>查看</a>
                                {{# if(d.Status  == 4){}}
                                <a class="layui-btn  layui-btn-disabled layui-btn-xs" lay-event="ex">发货</a>
                                {{# } else { }}
                                <a class="layui-btn layui-btn-xs" lay-event="ex">发货</a>
                                {{# } }}
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>取消</a>
                            </script>
                        }
                    </div>
                </div>
            </div>

            <div class="layui-fluid" id="detail" style="display:none; width:450px;">
                <div class="layui-row layui-col-space6">
                    <div class="layui-col-md12">
                        <div class="layui-card">
                            <div class="layui-card-body">
                                <div class="layui-form" action="" lay-filter="">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">商品</label>
                                        <div class="layui-form-mid" id="buyProduct"></div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">购买数量</label>
                                        <div class="layui-form-mid" id="buyCount"></div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">会员信息</label>
                                        <div class="layui-form-mid" id="user"></div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">金额</label>
                                        <div class="layui-form-mid" id="buyMoney"></div>
                                    </div>
                                    <fieldset class="layui-elem-field layui-field-title">
                                        <legend>收货信息</legend>
                                    </fieldset>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">收货人</label>
                                        <div class="layui-form-mid" id="receiver"></div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">联系电话</label>
                                        <div class="layui-form-mid" id="phone"></div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">收货地址</label>
                                        <div class="layui-form-mid" id="address"></div>
                                    </div>
                                    <fieldset class="layui-elem-field layui-field-title">
                                        <legend>汇款信息</legend>
                                    </fieldset>
                                    <div class="layui-form-item" id="divRemittanceImg">
                                        <label class="layui-form-label">汇款凭证</label>
                                        <div class="layui-form-mid"><a href="" target="_blank" id="aRemittanceImg"><img style="width:220px; height:120px" id="iRemittanceImg" title="点击查看大图" /></a></div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">汇款时间</label>
                                        <div class="layui-form-mid" id="dRemittanceTime"></div>
                                    </div>
                                    <fieldset class="layui-elem-field layui-field-title" id="express">
                                        <legend>快递信息</legend>
                                    </fieldset>
                                    <div id="expressList"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            @if ((int)ViewData["userType"] == (int)Code.UserTypes.普通用户) {
                var s = ViewData["setting"] as List<Models.SystemSetting>;
                <div class="layui-fluid" id="re" style="display:none;">
                    <div class="layui-row layui-col-space6">
                        <div class="layui-col-md12">
                            <div class="layui-card">
                                <div class="layui-card-body">
                                    <div class="layui-form" action="" lay-filter="">
                                        <div class="layui-form-item">
                                            <div class="layui-col-lg12">
                                                <label class="layui-form-label">转账方式</label>
                                                <div class="layui-input-block">
                                                    @foreach (var cash in cashs) {
                                                        <input type="radio" value="@cash.value" id="Remittance" name="Remittance" title="@cash.text" lay-filter="radio">
                                                    }
                                                </div>
                                            </div>
                                        </div>

                                        <div id="divBank">
                                            @if (s != null && s.Count > 0) {
                                                var name = s.FirstOrDefault(f => f.OtherType == ((int)Code.SystemType.收款人).ToString());
                                                var bank = s.FirstOrDefault(f => f.OtherType == ((int)Code.SystemType.收款银行).ToString());
                                                var account = s.FirstOrDefault(f => f.OtherType == ((int)Code.SystemType.收款账号).ToString());
                                                if (name != null) {
                                                    <div class="layui-form-item">
                                                        <label class="layui-form-label">收款人</label>
                                                        <div class="layui-form-mid">@name.Values</div>
                                                    </div>
                                                }
                                                @if (bank != null) {
                                                    <div class="layui-form-item">
                                                        <label class="layui-form-label">收款银行</label>
                                                        <div class="layui-form-mid">@bank.Values</div>
                                                    </div>
                                                }
                                                @if (account != null) {
                                                    <div class="layui-form-item">
                                                        <label class="layui-form-label">收款卡号</label>
                                                        <div class="layui-form-mid">@account.Values</div>
                                                    </div>
                                                }
                                            }
                                        </div>

                                        <div id="divAlipay" style="display:none;">
                                            @if (s != null && s.Count > 0) {
                                                var alipay = s.FirstOrDefault(f => f.OtherType == ((int)Code.CashType.支付宝).ToString());
                                                if (alipay != null) {
                                                    <div class="layui-form-item">
                                                        <label class="layui-form-label">收款人</label>
                                                        <div class="layui-upload">
                                                            <a href="javascript:;">
                                                                <img class="layui-upload-img" style="width:50%; height:50%" id="alipayImg" src="@alipay.Values">
                                                            </a>
                                                        </div>
                                                    </div>
                                                }
                                            }
                                        </div>
                                        <div id="divWechat" style="display:none;">
                                            @if (s != null && s.Count > 0) {
                                                var wechat = s.FirstOrDefault(f => f.OtherType == ((int)Code.CashType.微信).ToString());
                                                if (wechat != null) {
                                                    <div class="layui-form-item">
                                                        <label class="layui-form-label">收款人</label>
                                                        <div class="layui-upload">
                                                            <a href="javascript:;">
                                                                <img class="layui-upload-img" style="width:50%; height:50%" id="alipayImg" src="@wechat.Values">
                                                            </a>
                                                        </div>
                                                    </div>
                                                }
                                            }
                                        </div>
                                        @*<div class="layui-form-item">
                                                <label class="layui-form-label">汇款人</label>
                                                <div class="layui-input-inline"><input type="text" maxlength="20" id="Remittance" name="Remittance" placeholder="" lay-verify="" class="layui-input"></div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">汇款银行</label>
                                                <div class="layui-input-inline"><input type="text" maxlength="200" id="RemittanceBank" name="RemittanceBank" placeholder="" lay-verify="" class="layui-input"></div>
                                            </div>
                                            <div class="layui-form-item">
                                                <label class="layui-form-label">汇款号码</label>
                                                <div class="layui-input-inline"><input type="text" maxlength="100" id="RemittanceNo" name="RemittanceNo" placeholder="" lay-verify="" class="layui-input"></div>
                                            </div>*@
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">汇款凭证</label>
                                            <div class="layui-upload">
                                                <button type="button" class="layui-btn" id="uploadimg">上传图片</button><input class="layui-upload-file" type="file" accept="undefined">
                                                <input type="hidden" name="RemittanceImg" id="RemittanceImg" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            } else {
                <div class="layui-fluid" id="ex" style="display:none;">
                    <div class="layui-row layui-col-space6">
                        <div class="layui-col-md12">
                            <div class="layui-card">
                                <div class="layui-card-body">
                                    <div class="layui-form" action="" lay-filter="">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">快递公司</label>
                                            <div class="layui-input-inline"><input type="text" maxlength="20" id="ExpressName" name="ExpressName" placeholder="" lay-verify="" class="layui-input"></div>
                                        </div>
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">快递单号</label>
                                            <div class="layui-input-inline"><input type="text" maxlength="20" id="Mailno" name="Mailno" placeholder="" lay-verify="" class="layui-input"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
    <script src="~/layer/layui.js"></script>
    <script>
        layui.config({
            base: '../../layer/' //静态资源所在路径
        }).extend({
            index: 'index' //主入口模块
            }).use(['form', 'index', 'table', 'upload'], function () {
                var $ = layui.$
                    , upload = layui.upload
                    , form = layui.form
                    , table = layui.table;

                var ua = navigator.userAgent.toLowerCase();
                var screenwidth = window.screen.width;
                if (!/iphone|ipad|ipod/.test(ua)) {
                    $("#iosiframe").attr("scrolling", "auto");
                } else {
                    $('#iosiframe').width(screenwidth + 'px');
                }

                var uploadInst = upload.render({
                    elem: '#uploadimg'
                    , url: '@Url.Action("filesave", "api")'
                    , accept: 'images'
                    , size: 2048
                    , done: function (res, index, upload) {
                        console.log(res);
                        console.log(upload);
                        if (res.code == 0) {
                            $('#RemittanceImg').val(res.data.src);
                        } else {
                            window.parent.tools.alert(res.msg);
                        }
                    }
                });

                $.ajax({
                    url: '@Url.Action("getorderlistcolumns", "api",new { userType = ViewData["userType"] })',
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'get'
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    alert(errorThrown, { icon: 2, title: textStatus });
                }).done(function (res) {
                    //res.splice(0, 0, { type: 'checkbox' });
                    res.push({ width: 205, align: 'center', toolbar: '#buttonTpl' });

                    var tableIns = table.render({
                        elem: '#table-page'
                        , cellMinWidth: 80
                        , limit: 30
                        , url: '@Url.Action("orderlist","api", new { userId = ViewData["Id"] })'
                        , toolbar: true
                        , defaultToolbar: ['filter', 'print', 'exports']
                        , cols: [res]
                        , parseData: function (res) {
                            return {
                                code: 0,
                                msg: "",
                                count: res.count,
                                data: JSON.parse(res.data)
                            }
                        }
                        , page: true
                    });

                    form.on('submit(LAY-user-back-search)', function (data) {
                        var field = data.field;

                        //执行重载
                        tableIns.reload({
                            where: field
                        });
                    });

                    var del = function (ids, table) {
                        $.ajax({
                            url: '@Url.Action("delorder", "api")'
                            , dataType: 'json'
                            , data: { ids: ids }
                            , contentType: 'application/json'
                            , type: 'get'
                        }).fail(function (jqXHR, textStatus, errorThrown) {
                            window.parent.tools.alert(errorThrown, { icon: 2, title: textStatus });
                        }).done(function (res) {
                            window.parent.tools.msg(res.message, { offset: '15px', icon: (res.code == 0 ? 1 : 2), time: 1000 });

                            if (res.code == 0) {
                                table.reload();
                            }
                        });
                    }


                    var active = {
                        batchdel: function () {
                            var checkStatus = table.checkStatus('table-page')
                                , checkData = checkStatus.data;

                            if (checkData.length === 0) {
                                return layer.msg('请选择数据');
                            }

                            window.parent.tools.confirm('确定要取消订单吗？', function () {
                                var ids = [];

                                checkData.forEach(function (item) { ids.push(item.Id) });

                                del(ids.join(','), tableIns);
                            });
                        }, add: function (obj) {

                        }
                    }

                    if ($('input[name="Remittance"]').html() != undefined) {
                        cashInfo = $('input[name="Remittance"]:checked').val();

                        if (cashInfo == undefined) {
                            $("input[name='Remittance']").get(0).click();

                            form.render('radio');
                        }
                    }

                    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });

                    table.on('tool(table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                        var data = obj.data; //获得当前行数据
                        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

                        if (layEvent === 'del') { //删除
                            window.parent.tools.confirm('确定要取消订单吗？', function (index) {
                                del(data.Id, tableIns);
                            });
                        } else if (layEvent === 're') {
                            if (data.Status == 2) {
                                window.parent.tools.confirm('你已经填写了汇款信息，需要修改汇款信息吗？', function (index) {
                                    layer.close(index);
                                    location.href = '@Url.Action("remittance","order")/' + data.Id;
                                    //Remittance(data, tableIns);
                                });
                            } else {
                                location.href = '@Url.Action("remittance","order")/' + data.Id;
                                //Remittance(data, tableIns);
                            }
                        } else if (layEvent === 'ex') {
                            if (data.Status == 3) {
                                window.parent.tools.confirm('你已经发过货了，是否要继续增加快递信息？', function (index) {
                                    layer.close(index);
                                    ExpressInfo(data, tableIns);
                                });
                            } else {
                                if (data.RemittanceImg.length == 0) {
                                    window.parent.tools.confirm('该会员还未汇款或汇款信息未填写完整，是否要发货？', function (index) {
                                        layer.close(index);
                                        ExpressInfo(data, tableIns);
                                    });
                                }
                                else {
                                    ExpressInfo(data, tableIns);
                                }
                            }
                        } else if (layEvent === 'look') {
                            orderInfo(data, tableIns);
                        }
                    });

                    var orderInfo = function (data, table) {
                        $.ajax({
                            url: '@Url.Action("orderinfo", "api")?id=' + data.Id,
                            dataType: 'json'
                        }).fail(function (jqXHR, textStatus, errorThrown) {
                            window.parent.tools.alert(errorThrown, { icon: 2, title: textStatus });
                        }).done(function (res) {
                            if (res) {
                                $('#buyProduct').text(res.ProductName);
                                $('#buyCount').text(res.Count);
                                $('#user').text(res.UserName + '(' + res.UserCode + '）');
                                $('#buyMoney').text(res.AllPrice);

                                $('#receiver').text(res.Receiver);
                                $('#phone').text(res.Phone);
                                $('#address').text(res.Address);


                                if (res.RemittanceImg && res.RemittanceImg.length > 0) {
                                    $('#dRemittanceTime').text(res.RemittanceTime);
                                    $('#iRemittanceImg').attr('src', res.RemittanceImg);
                                    $('#aRemittanceImg').attr('href', res.RemittanceImg);
                                    $('#divRemittanceImg').show()
                                } else {
                                    $('#divRemittanceImg').hide()
                                }

                                var expList = res.ExpressInfos;

                                if (expList && expList.length > 0) {
                                    var html = '';
                                    expList.forEach(function (item) {
                                        html += '<div class="layui-form-item">\
                                                    <label class="layui-form-label">快递公司</label>\
                                                    <div class="layui-form-mid">'+ item.ExpressName + '</div>\
                                                 </div>\
                                                 <div class="layui-form-item">\
                                                    <label class="layui-form-label">快递单号</label>\
                                                    <div class="layui-form-mid">'+ item.Mailno + '</div>\
                                                 </div>\
                                                 <div class="layui-form-item">\
                                                    <label class="layui-form-label">发货时间</label>\
                                                    <div class="layui-form-mid">'+ item.CreateTime + '</div>\
                                                 </div><hr />';
                                    });

                                    $('#expressList').html(html);

                                    $('#express').show();
                                } else {
                                    $('#expressList').html('');
                                    $('#express').hide();
                                }

                                window.parent.tools.open($('#detail').html(), '订单详情', ['确定'], ['420px', '520px'], null, function (index) {
                                    window.parent.tools.close(index);
                                });
                            }
                        });
                    }

                    $('.layui-upload > a').on('click', function () {
                        var src = $(this).children('img').attr('src');

                        window.parent.tools.photos(src, '收款图');
                    });

                    form.on('radio(radio)', function (data) {
                        if (data.value == 1) {
                            $('#divBank').show();
                            $('#divAlipay').hide();
                            $('#divWechat').hide();
                        }
                        else if (data.value == 2) {
                            $('#divBank').hide();
                            $('#divAlipay').show();
                            $('#divWechat').hide();
                        }
                        else {
                            $('#divBank').hide();
                            $('#divAlipay').hide();
                            $('#divWechat').show();
                        }
                    });

                    var ExpressInfo = function (data, table) {
                        window.parent.tools.open($('#ex').html(), '快递信息', ['提交', '取消'], null, ['ExpressName', 'Mailno'], function (index, values) {
                            var layerIndex = index;
                            var loadindex = window.parent.tools.load(1, { shade: 0.8 });

                                $.ajax({
                                    url: '@Url.Action("saveexpress","api")',
                                    dataType: 'json',
                                    contentType: 'application/json',
                                    data: JSON.stringify({ Id: data.Id, ExpressName: values.ExpressName, Mailno: values.Mailno }),
                                    type: 'post'
                                }).fail(function (jqXHR, textStatus, errorThrown) {
                                    window.parent.tools.alert(errorThrown, { icon: 2, title: textStatus });
                                }).done(function (res) {
                                    window.parent.tools.msg(res.message, { offset: '15px', icon: (res.code == 0 ? 1 : 2), time: 1000 }, function (index) {
                                        if (res.code == 0) {
                                            table.reload();
                                            $('#ExpressName').val('');
                                            $('#Mailno').val('');
                                            window.parent.tools.close(layerIndex);
                                        }
                                        window.parent.tools.close(loadindex);
                                    });
                                });
                        });
                        @*var layerIndex = layer.open({
                            type: 1,
                            title: '快递信息',
                            closeBtn: false,
                            shade: 0.8,
                            area: ['420px'],
                            content: $('#ex'),
                            btn: ['提交', '取消'],
                            btnAlign: 'c',
                            yes: function () {
                                var ExpressName = $('#ExpressName').val(),
                                    Mailno = $('#Mailno').val();


                            }
                        });*@
                    }

                    var Remittance = function (data, table) {
                        //$('#Remittance').val(data.Remittance);
                        //$('#RemittanceBank').val(data.RemittanceBank);
                        //$('#RemittanceNo').val(data.RemittanceNo);
                        //$('#RemittanceImg').val(data.RemittanceImg);

                        var layerIndex = layer.open({
                            type: 1,
                            title: '汇款信息',
                            closeBtn: false,
                            shade: 0.8,
                            area: ['420px'],
                            content: $('#re'),
                            btn: ['提交', '取消'],
                            btnAlign: 'c',
                            yes: function () {
                                var remittance = $('#Remittance').val()
                                    , bank = $('#RemittanceBank').val()
                                    , no = $('#RemittanceNo').val()
                                    , img = $('#RemittanceImg').val();

                                $.ajax({
                                    url: '@Url.Action("saveremittance","api")',
                                    dataType: 'json',
                                    contentType: 'application/json',
                                    data: JSON.stringify({ Id: data.Id, Remittance: remittance, RemittanceBank: bank, RemittanceNo: no, RemittanceImg: img }),
                                    type: 'post'
                                }).fail(function (jqXHR, textStatus, errorThrown) {
                                    window.parent.tools.alert(errorThrown, { icon: 2, title: textStatus });
                                }).done(function (res) {
                                    window.parent.tools.msg(res.message, { offset: '15px', icon: (res.code == 0 ? 1 : 2), time: 1000 }, function (index) {
                                        if (res.code == 0) {
                                            table.reload();
                                            $('#Remittance').val('');
                                            $('#RemittanceBank').val('');
                                            $('#RemittanceNo').val('');
                                            layer.close(layerIndex);
                                        }
                                    });
                                });
                            }
                        });
                    }
                });
            });
    </script>
</body>
</html>
